// run: $exec < input
#include <iostream>
#include <algorithm>
#include <iterator>
#include <set>
#include <cstring>

char a[10][10];
std::set<int> po[10][10];

void check_row()
{
	for (int i = 1; i <= 4; i++) {
		std::set<int> s{1, 2, 3, 4};
		for (int j = 1; j <= 4; j++) {
			if (a[i][j] != '*') {
				s.erase(s.find(a[i][j] - '0'));
			}
		}
		for (int j = 1; j <= 4; j++)
			if (a[i][j] == '*') {
				std::set<int> tmp;
				std::set_intersection(po[i][j].begin(), po[i][j].end(),
									  s.begin(), s.end(),
									  std::inserter(tmp, tmp.begin()));
				po[i][j] = tmp;
				if (po[i][j].size() == 1) {
					a[i][j] = *po[i][j].begin() + '0';
				}
			}
	}
}

void check_col()
{
	for (int j = 1; j <= 4; j++) {
		std::set<int> s{1, 2, 3, 4};
		int count = 0;
		for (int i = 1; i <= 4; i++) {
			if (a[i][j] != '*') {
				s.erase(s.find(a[i][j] - '0'));
				count++;
			}
		}
		for (int i = 1; i <= 4; i++)
			if (a[i][j] == '*') {
				std::set<int> tmp;
				std::set_intersection(po[i][j].begin(), po[i][j].end(),
									  s.begin(), s.end(),
									  std::inserter(tmp, tmp.begin()));
				po[i][j] = tmp;
				if (po[i][j].size() == 1)
					a[i][j] = *po[i][j].begin() + '0';
			}
	}
}

int const opt[4][2] = {{1, 1}, {1, 0}, {0, 1}, {0, 0}};

void check_square()
{
	for (int i = 1; i <= 4; i += 2) {
		for (int j = 1; j <= 4; j += 2) {
			std::set<int> s{1, 2, 3, 4};
			for (int k = 0; k < 4; k++) {
				int ti = i + opt[k][0];
				int tj = j + opt[k][1];
				if (a[ti][tj] != '*') {
					s.erase(s.find(a[ti][tj] - '0'));
				}
			}
			for (int k = 0; k < 4; k++) {
				int ti = i + opt[k][0];
				int tj = j + opt[k][1];
				if (a[ti][tj] == '*') {
					std::set<int> tmp;
					std::set_intersection(po[ti][tj].begin(), po[ti][tj].end(),
										  s.begin(), s.end(),
										  std::inserter(tmp, tmp.begin()));
					if (po[ti][tj].size() == 1)
						a[ti][tj] = *po[ti][tj].begin() + '0';
				}
			}
		}
	}
}

bool check()
{
	for (int i = 1; i <= 4; i++) {
		std::set<int> s;
		for (int j = 1; j <= 4; j++) {
			if (s.find(a[i][j] - '0') != s.end()) return false;
			s.insert(a[i][j] - '0');
		}
	}

	for (int j = 1; j <= 4; j++) {
		std::set<int> s;
		for (int i = 1; i <= 4; i++) {
			if (s.find(a[i][j] - '0') != s.end()) return false;
			s.insert(a[i][j] - '0');
		}
	}

	for (int i = 1; i <= 4; i += 2) {
		for (int j = 1; j <= 4; j += 2) {
			std::set<int> s;
			for (int k = 0; k < 4; k++) {
				int ti = i + opt[k][0];
				int tj = j + opt[k][1];
				if (s.find(a[ti][tj] - '0') != s.end()) return false;
				s.insert(a[ti][tj] - '0');
			}
		}
	}

	return true;
}

bool dfs(int d)
{
	if (d >= 16) {
		if (check()) {
			for (int i = 1; i <= 4; i++) {
				for (int j = 1; j <= 4; j++)
					std::cout << a[i][j];
				std::cout << "\n";
			}
			return true;
		} else return false;
	}
	int x = d / 4, y = d % 4;
	x++; y++;
	if (a[x][y] != '*') {
		if (dfs(d + 1)) return true;
	} else {
		for (auto i : po[x][y]) {
			a[x][y] = i + '0';
			if (dfs(d + 1)) return true;
			a[x][y] = '*';
		}
	}
	return false;
}

int main()
{
	std::ios::sync_with_stdio(false);
	int t; std::cin >> t;
	for (int ti = 1; ti <= t; ti++) {
		std::cout << "Case #" << ti << ":\n";
		for (int i = 1; i <= 4; i++)
			for (int j = 1; j <= 4; j++) {
				std::cin >> a[i][j];
				if (a[i][j] == '*') po[i][j] = std::set<int>{1, 2, 3, 4};
				else po[i][j] = std::set<int>{a[i][j] - '0'};
			}

		for (int i = 0; i < 100; i++) {
			check_row();
			check_col();
			check_square();
		}

		dfs(0);

	}
}

